# -*- coding: utf-8 -*-

import sys
import os
import requests
import json
import time
import re
import traceback,threading
import MySQLdb
import datetime

print('>>>正在初始化...')

DataBase = "sccplat"
proDir = os.path.split(os.path.realpath(__file__))[0]
SQL_CONNEC_URL = 'rm-bp1z598ttm5ahi9o15o.mysql.rds.aliyuncs.com'
SQL_USER_NAME = 'wangjun'
SQL_PWD = 'wj123456'
SQL_PORT = 3306
print('proDir---',proDir)
proDir = proDir.replace('\dist','')
import time
from selenium import webdriver
def km1kt(browser=None):
    try:
        
        browser.get('http://exam.jsyst.cn/km1/kt/')
        time.sleep(1)

        timu_nums = 1
        while timu_nums<2:
            id_qus = browser.find_element_by_id('id_qus')
            print('ques--',id_qus.text)
            # sts = browser.find_element_by_xpath('//div[@class="st"]')
            sts = browser.find_elements_by_xpath('//div[@class="st"]/p')
            for index,item in enumerate(sts):
                if index>0:
                    print('st--',index,'---',item.text)

            id_js = browser.find_element_by_id('id_js')
            print('id_js--',id_js.text)

            id_ans = browser.find_element_by_id('id_ans')
            print('id_ans--',id_ans.text)

            id_bmp = browser.find_element_by_xpath('//div[@id="id_bmp"]/img')
            print('图片id_bmp--',id_bmp)

            btns = browser.find_elements_by_xpath('//div[@class="centerkt fl"]/dl/dd/a')
            time.sleep(2)
            print('btns--',btns[1].click())
            timu_nums +=1
    except:
        traceback.print_exc()
        print('异常处理')
    
    

class MainThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):

        ####linux下开始
        options = webdriver.FirefoxOptions()
        options.add_argument('--headless')
        #linux下结束

        ####window下开始
        #创建一个浏览器对象,windows
        # profile=webdriver.FirefoxProfile()
        # profile.accept_untrusted_certs=True
        # geckodriverPath = os.path.join(proDir, "geckodriver")
        # browser=webdriver.Firefox(firefox_profile=profile,executable_path=geckodriverPath) 
        ####window下结束
        while True:
            try:
                browser = webdriver.Firefox(firefox_options=options)#linux
                # browser=webdriver.Firefox(firefox_profile=profile,executable_path=geckodriverPath) #windows版
                curr_time = datetime.datetime.now()
                print("线程在跑----",curr_time.strftime('%Y-%m-%d %H:%M:%S'))
                km1kt(browser)

            except Exception as e:
                print('登录主线程报错，继续执行。。。。')
                traceback.print_exc()
            
            time.sleep(300)


import configparser
if __name__ == '__main__':
    
    # MainThread().start()
    # print('执行成功')
    # time.sleep(2)

    
    try:
        browser = None
        profile=webdriver.FirefoxProfile()
        profile.accept_untrusted_certs=True
        geckodriverPath = os.path.join(proDir, "geckodriver")#将驱动放在同目录下
        browser=webdriver.Firefox(firefox_profile=profile,executable_path=geckodriverPath) #windows版
        
        km1kt(browser)
    except Exception as e:
        print('登录主线程报错，继续执行。。。。')
        traceback.print_exc()

